(function($) {
    var loaderObject;
    celebrioLoader = function(name, image, hf, parent) {

        this.name = name;
        this.parent = parent;
        this.image = image;
        this.headerFooter = hf;
		
		this.layOut = function() {
            $("#apploading").css({
                top : $(window).height() / 2 -  $("#apploading img").height()/2,
                left : (($(window).width() - $("#apploading").width()) / 2)
            });

            $("#stableloading").css({
                top : $(window).height() / 2 - $("#stableloading img").height()/2,
                left : (($(window).width() - $("#stableloading").width()) / 2)
            });  
        }
        
        this.create = function() {      
            /**
             * var dpi - global variable defined in system_app_layout.phtml 
             * var basePath - global variable defined in system_app_layout.phtml 
             */
            $(parent).prepend("<div id='dummy_layout'></div>");
            spinner = basePath + "/images/seniors/progressbar/"+ dpi +"dpi/icon.png";
                        
            $("#dummy_layout").append("<div id='loader'>" + "<div id='apploading'>" + "<img src='"+spinner+"'>" + "</div>" + "<div id='stableloading'>" + "<img src='" + this.image + "'>" + "</div>" + "</div>");
            $("#dummy_layout").append("<div id='overlay'></div>");
      
            $("#loader").css({
                visibility : "hidden",
                overflow: "hidden"
            });
            
            $("#overlay").css({
                position : "absolute",
                top : 0,
                width : $(window).width(),
                height : $(window).height(),
                zIndex : 1999,
                background : 'none repeat scroll 0 0 rgba(51, 51, 51, 0.8)',
                overflow: "hidden"
            });       
            
            $(window).resize(function(){
                
                $("#overlay").height($(window).height());
                $("#overlay").width($(window).width());
                
                var items = $("#stableloading").add($("#apploading"));
                
                $.each(items,function(){
                    $(this).css({
                        top : ($(window).height() - $(this).height())/ 2 ,
                        left : (($(window).width() - $(this).width()) / 2),
                        overflow: "hidden"
                    });
                });       
            });          

            if(this.headerFooter) {
                $("#dummy_layout").append("<div id='dummyheader'>" + "<div id='dummy_header_left'>" + "<h1>" + $(this.name).text() + "</h1>" + "</div>" + "<div id='dummy_header_right'>" + "<div id='dummy_scrollbar'></div>" + "</div>" + "</div>" + "<div id='dummyfooter'>" + "<a class='nonactive' id='dummyfooter_left'></a>" + "<a href='#' id='dummyfooter_right' class='exit_application'  style='text-decoration: none'>" + "<h1>"+ exitLabel + "</h1></a></div>");
                $("#dummyfooter_right").mousedown(function(button) {
                    if (button.which == 1) {
                        $("#dummy_layout").fadeOut("slow");

                        $(".dialogApplication").celebrioApp("minimize");
                        $(".albireoicon").fadeIn("slow");
                        $("#taskbar").fadeIn("slow");
                        $("#scrollbar").fadeIn("slow");
                        //$('#startbutton').fadeOut("slow"); // there is no #startbutton in whole system
                        (window).focus();
                        //Set focus back to webtop
                        celebrio.desktop.icons.resize();
                    }

                });
                $("#dummyfooter").css({
                    overflow : "hidden",
                    position : 'absolute',
                    bottom : 0,
                    width : "100%",
                    height : 80,
                    zIndex : 2000,
                    fontFamily : "XXIISinozDSPMedium, Trebuchet MS, Geneva CE, lucida, sans-serif",
                });

                $("#dummyfooter h1").css({
                    padding : "10px 20px 0px 20px",
                    marginTop : 0,
                    fontSize : "3em",
                    fontFamily : "XXIISinozDSPRegular, Trebuchet MS, Geneva CE, lucida, sans-serif",
                    fontWeight: "normal"
                });

               $("#dummyfooter_left").css({
                    border : "1px solid rgba(51,51,51,1.0)",
                    border : "hsl(0, 0%, 50%) solid 1px",
                    borderTopRightRadius: "10px",   
                    float : "left",
                    width : "49.6%",
                    height : "100%",
                    background : 'url("'+basePath+'/images/seniors/gradients/verticalE0E0E0alpha10to00.png")',
                    opacity : 0.3
                });

                $("#dummyfooter_right").css({
                    border : "1px solid rgba(51,51,51,1.0)",
                    border : "hsl(0, 0%, 50%) solid 1px",
                    borderTopLeftRadius : "10px",   
                    float : "right",
                    width : "49.6%",
                    height : "100%",
                    color : "#333333",
                    background : 'url("'+basePath+'/images/seniors/gradients/verticalE0E0E0alpha10to00.png")',
                    textAlign : 'right',
                    opacity: 0.9
                });

                $("#dummyheader").css({
                    width : "100%",
                    height : 100,
                    zIndex : 2000,
                    position : "absolute",
                    top : 0,
                    fontFamily : "XXIISinozDSPRegular, Trebuchet MS, Geneva CE, lucida, sans-serif",
                    background : 'url("'+basePath+'/images/seniors/gradients/vertical333333alpha08to00.png")'
                });

                $("#dummyheader h1").css({
                    fontWeight: "normal",
                    padding : "10px 20px 0px 20px",
                    marginTop : 0,
                    fontSize : "4em",
                    color : "#FFFFFF"
                });

                $("#dummy_header_left").css({
                    float : "left",
                    maxWidth : "50%",
                    minWidth : "300px",
                    marginRight : 20,
                    height : 140,
                    verticalAlign : "text-bottom",
                    overflow : "hidden",
                    whiteSpace : "nowrap"
                });

                $("#dummy_header_left").css({
                    height : 140
                });

                $("#dummy_scrollbar").css({
                    position : "relative",
                    top : 0,
                    width : "auto",
                    height : 80,
                    overflow : "hidden",
                    zIndex : 2001,
                    background : 'url("'+basePath+'/images/seniors/ScrollbarBackground.png")'
                });
            }
			
            $("#apploading").css({
                position : "absolute",
                zIndex : 10000
            });
			
            $("#stableloading").css({
                position : "absolute",
                zIndex : 10000
            });
			
			$("#dummy_layout").bind('dragstart', function(event) {
			  event.preventDefault();
			});
			
            counter = 0;
            refreshIntervalId = this.start();
        };
		
        this.refresh = function() {
            $("#overlay").css({
                width : $(window).width(),
                height : $(window).height(),
            });
        }
		
        this.remove = function() {
            this.stop();
            $("#dummy_layout").fadeOut("slow");
            setTimeout(function() {
                $("#dummy_layout").remove();
            }, 600);
        }
                
        this.rotate = function() {
            loaderObject.layOut();
            
            $("#apploading img").css({
                'transform' : 'rotate(' + counter + 'deg)',
                '-moz-transform' : 'rotate(' + counter + 'deg)',
                //in new jQuery 1.7.0, this attribute should be -ms-transform
                'ms-transform' : 'rotate(' + counter + 'deg)',
                '-webkit-transform' : 'rotate(' + counter + 'deg)'
            });	
            counter = (counter + 3) % 360 ;
        }
        
        this.start = function() {
            loaderObject = this;
            this.layOut();
            setTimeout(function(){
                if(this.headerFooter)
                    $("#loader").fadeIn("slow");
                else
                    $("#loader").css("visibility", "visible");         
                },100);
            return setInterval(this.rotate, 30);
        }
        
        this.stop = function() {
            clearInterval(refreshIntervalId);
        }	
    }
})(jQuery);